using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Hotran.Audio;
using DG.Tweening;

public class litterBox : MonoBehaviour
{
    [SerializeField] Game game;
    [SerializeField] PackageSelector packageSelector;

    [SerializeField] SpriteRenderer dropPrefab;
    [SerializeField] AudioClip trashBinSound;

    // Start is called before the first frame update
    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (!game.Started) return;
        if (coll.TryGetComponent(out Package package))
        {
            AudioPlayer.Instance.PlaySound(trashBinSound);
            ShowWrong();

            packageSelector.RemovePackage(package);
            package.Drop();
            game.Health--;
        }
    }
    
    private void ShowWrong()
    {
        var wrong = Instantiate(dropPrefab,
            transform.position + Vector3.up * 1f,
            Quaternion.identity);
        var transparent = Color.white;
        transparent.a = 0f;
        wrong.color = transparent;
        var sequence = DOTween.Sequence();
        sequence.Append(wrong.DOFade(1f, 0.2f))
            .Append(wrong.DOFade(0f, 0.3f))
            .OnComplete(() => Destroy(wrong.gameObject));
    }
}
